using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using Meditek.Entities;
using Meditek.UI.Controls;
using Meditek.UI._ProceduresReference;
using Meditek.UI.PersonProceduresReference;
using Meditek.UI.PersonsReference;

namespace Meditek.UI
{
    public partial class PersonProceduresList : System.Web.UI.Page
    {

		#region Private Variables
        
		public ReferenceConverter referenceConverter = new ReferenceConverter();
		private PersonProceduresServiceClient _personProceduresServiceClient = null;
		
		#endregion
        
		#region Page Events
        
        protected void Page_Load(object sender, EventArgs e)
        {
			if (!Page.IsPostBack)
			{
				(this.Master.FindControl("Logo").FindControl("lblFormTitle") as Label).Text = "PersonProcedures List";
				_ProceduresServiceClient _ProceduresServiceClient = new _ProceduresServiceClient();
				ReferenceConverter._ProceduresList = _ProceduresServiceClient.GetAll();
				PersonsServiceClient personsServiceClient = new PersonsServiceClient();
				ReferenceConverter.PersonsList = personsServiceClient.GetAll();
				btnDelete.Attributes.Add("onclick", "return confirm('Are you sure you want to delete this record?');");
				PrepareSearchUserControl();
			}
			ucSearch.Search += new SearchUserControl.SearchEventHandler(ucSearch_Search);
        }
        
        protected void PersonProceduresDataSource_ObjectCreated(object sender, ObjectDataSourceEventArgs e)
        {
            _personProceduresServiceClient = e.ObjectInstance as PersonProceduresServiceClient;
        }
        
        protected void PersonProceduresDataSource_Deleted(object sender, ObjectDataSourceStatusEventArgs e)
        {
            try
            {
                _personProceduresServiceClient.CommitChanges();
                (this.Master.FindControl("Information").FindControl("msglblError") as MessageLabel).ShowInfo("Item Deleted Successfully!", false);
            }
            catch (Exception ex)
            {
				(this.Master.FindControl("Information").FindControl("msglblError") as MessageLabel).ShowInfo(ex.Message, true);
            }
        }
        
        protected void ucSearch_Search(object sender, SearchUserControl.SearchEventArgs e)
        {
            PersonProceduresDataSource.SelectParameters["conditionString"].DefaultValue = e.ConditionString;
            PersonProceduresDataSource.Select();
            gvPersonProcedures.DataBind();
        }
        
        protected void gvPersonProcedures_RowCommand(object sender, GridViewCommandEventArgs e)
        {
        }

		protected void btnInsert_Click(object sender, EventArgs e)
        {
			Response.Redirect("PersonProceduresForm.aspx?mode=Insert");
        }
        
        protected void btnDelete_Click(object sender, EventArgs e)
        {
			_personProceduresServiceClient = new PersonProceduresServiceClient();
			PersonProcedures personProcedures = new PersonProcedures();
			bool deleteStatus = false;
			
            foreach (GridViewRow item in gvPersonProcedures.Rows)
            {
                CheckBox chk = item.FindControl("chkSelect") as CheckBox;
                if (chk.Checked)
                {
					deleteStatus = true;
                    personProcedures.idPersonProcedure = Convert.ToInt32(gvPersonProcedures.DataKeys[item.RowIndex].Value.ToString());
                    _personProceduresServiceClient.Delete(personProcedures);
                }
            }
            
            if (deleteStatus)
            {
				try
                {
                    _personProceduresServiceClient.CommitChanges();
                    (this.Master.FindControl("Information").FindControl("msglblError") as MessageLabel).ShowInfo("Items Deleted Successfully!", false);
                }
                catch (Exception ex)
                {
					(this.Master.FindControl("Information").FindControl("msglblError") as MessageLabel).ShowInfo(ex.Message, true);
                }
                gvPersonProcedures.DataBind();
            }
        }
        
		#endregion
        
		#region Helper Functions
        
        private void PrepareSearchUserControl()
        {
            Dictionary<string, string> membersList = new Dictionary<string, string>();
			membersList.Add("idPersonProcedure", "idPersonProcedure");
			membersList.Add("EndDate", "EndDate");
			membersList.Add("idProcedure", "idProcedure");
			membersList.Add("Person", "Person");
			membersList.Add("StartDate", "StartDate");
			ucSearch.AddMembers(membersList);
        }
        
		#endregion
		
		#region Custom Code
		
		
		#endregion
        
    }
}
